from typing import List, Union

from pydantic import BaseSettings, AnyHttpUrl, validator


class Settings(BaseSettings):
    """
    配置文件项目参数
    """
    LOGIN_URL: str = "https://oc.chengtou.oc.com:26335"
    USERNAME: str = "ecdataadmin"
    GRANTTYPE: str = "password"
    VALUE: str = "Ecdata@123"
    DEFAULT_PREFIX: str = "/api"
    TOKEN_EXPIRE_TIME: int = 3600 * 24

    PROJECT_NAME = "water"

    BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = []  # 允许访问的前端IP以及端口

    @validator("BACKEND_CORS_ORIGINS", pre=True)
    def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]:
        if isinstance(v, str) and not v.startswith("["):
            return [i.strip() for i in v.split(",")]
        elif isinstance(v, (list, str)):
            return v
        raise ValueError(v)

    class Config:
        case_sensitive = True


settings = Settings()
